<?php
/**
 * @name User
 * @package: User
 * @package public
 * @author Truonghm
 * @version $1.0
 * @license http://officex.vn/license
 * @copyright officex.
 * @date 24/3/2011
 * cac ham nghiep vu lien quan den role
 * 
 */
class Role_Models_Business_Role extends Role_Models_Interface_Role {
	/**
	 @author Truonghm
	 @version $1.0
	 @desc Lay toan bo nhom quyen trong bang Role
	 */
	protected function doGetList($json){
		$data = Zend_Json::decode($json, Zend_Json::TYPE_OBJECT);
		$limit = $data->limit;
		$start = $data->start;
		$query = $data->query;
		
		$model = new Models_Role();
		
		$select = $model->select()
						->where('name like \'%'.$query.'%\'')
						->order('created_date DESC')
						->limit($limit,$start);
						
		$flag["count"] = $this->getRoleCount($query);
		$flag["data"]=$model->fetchAll($select)->toArray();
		return Zend_Json::encode($flag);
	}
	
	protected function getRoleCount($query=null){
		$table = new Models_Role();
		$select = $table->select()
						->where('name like \'%'.$query.'%\'')
					    ->from($table,'COUNT(*) AS count');		
		$row=$table->fetchRow($select);
		return $row->count;
	}
	
	protected function doUpdate($json){
		
		$obj = Zend_Json::decode($json, Zend_Json::TYPE_OBJECT);
		$db = Zend_Registry::get('db');
		
		$edit_id = $obj->columns->id;
		
		try {	
			$db->beginTransaction();
			
			$data = (array)$obj->columns;
			
			$table = new Models_Role();
			$where = $table->getAdapter()->quoteInto('id = ?', $edit_id);
			$result = $table->update($data,$where);			
			
			$db->commit();
			
			$flag["success"] = true;
			$flag["errors"] = "";
			
		} catch (Exception $e){
			$db->rollBack();
			$flag["success"] = false;
			$flag["data"]["errors"] = $e->getMessage();
		}
		
		return Zend_Json::encode($flag);		
	}
	
	protected function doGetPrimarykey($role_id)
	{
		try {
			$lblRole = new Models_Role();		
			$data = $lblRole->find($role_id);
			$data = $data->current()->toArray();
			
			$flag["success"] = true;
			$flag["data"] = $data;
		} catch (Exception $e){
			$flag["success"] = false;
			$flag["errors"] = $e->getMessage();
		}
		return 	Zend_Json::encode($flag);	  
	}
	
	protected function doInsert($json){
		$obj = Zend_Json::decode($json, Zend_Json::TYPE_OBJECT);
		$translate=Zend_Registry::get('translate');
		
		$db = Zend_Registry::get('db');
		
		try {	
			$db->beginTransaction();
			
			$auth = Zend_Auth::getInstance();
			$userInfo = $auth->getIdentity();
		
			$lblRole = new Models_Role();
			
			$data = (array)$obj->columns;
			$data["created_by"] = $userInfo->username;			
			$data["created_date"] = new Zend_Db_Expr('SYSDATE()');	
			
			$result= $lblRole->insert($data);
			
			$db->commit();
			
			$flag["success"] = true;
						
		} catch (Exception $e){
			$db->rollBack();
			$flag["success"] = false;
			$flag["data"]["errors"] = $e->getMessage();
		}
		return Zend_Json::encode($flag);		
	}
	
	protected function doDelete($role_id){
		$db = Zend_Registry::get('db');
		try {	
			if(!$role_id)
			{
				$flag["success"] = false;
				$flag["errors"] = "";
				return Zend_Json::encode($flag);
			}
			$db->beginTransaction();
			
			$lblRole = new Models_Role();
			$where = $lblRole->getAdapter()->quoteInto('id = ?', $role_id);
			$result = $lblRole->delete($where);
			
			$lblUserRole = new Models_UserRole();
			$where = $lblUserRole->getAdapter()->quoteInto('role_id = ?', $role_id);
			$result = $lblUserRole->delete($where);
			
			$lblRoleMenuPrivilege = new Models_RoleMenuPrivilege();
			$where = $lblRoleMenuPrivilege->getAdapter()->quoteInto('role_id = ?', $role_id);
			$result = $lblRoleMenuPrivilege->delete($where);			
			
			$db->commit();
			
			$flag["success"] = true;
						
		} catch (Exception $e){
			$db->rollBack();
			$flag["success"] = false;
			$flag["errors"] = $e->getMessage();
		}
		return Zend_Json::encode($flag);
	}
	/**
	 * @author truonghm
	 * created: 04/04/2011
	 * @desc tong hop du lieu menu va privilege xuat du lieu dang tree
	 */
	protected function doGetRolePrivilege(){
		
		$lblMenu = new Models_Menu();
		$lblMenuPrivilege = new Models_MenuPrivilege();
		$lblPrivilege = new Models_Privilege();
		
		$select = $lblMenu->select()
						->setIntegrityCheck(false)
						->from(array("m"=>$lblMenu->info("name")) ,"id")
						->joinInner(array("mp"=>$lblMenuPrivilege->info("name")),"m.id =mp.menu_id",array("mp.id as mp_id"))
						->joinInner(array("p"=>$lblPrivilege->info("name")),"mp.privilege_id = p.id",array("p.display_name"));
		$arrPrivilege = $lblMenu->fetchAll($select)->toArray();
		
		$flag["success"] = true;
		$flag["data"] = $arrPrivilege;
		return 	Zend_Json::encode($flag); 
		
	}
	
	protected function doGetRolePrivileges($role_id){
		$lblRole = new Models_Role();
		$lblRoleMenuPrivilege = new Models_RoleMenuPrivilege();
		$select = $lblRole->select()
						->setIntegrityCheck(false)
						->from(array("r"=>$lblRole->info("name")) ,array("r.id","r.name"))
						->joinLeft(array("rmp"=>$lblRoleMenuPrivilege->info("name")),"r.id = rmp.role_id",array("rmp.menu_privilege_id"))
						->where("r.id=?",$role_id);
		$data = $lblRole->fetchAll($select);
		
		if(count($data)>0){
			foreach ($data as $key=>$value){
				$privileges[] = $value["menu_privilege_id"];
				$roleName = $value["name"];
			}
			
			$flag["success"] = true;
			$flag["data"]["id"] =$role_id;
			$flag["data"]["name"] =$roleName;
			$flag["data"]["privilege"] = $privileges;
		}else{
			$translate=Zend_Registry::get('translate');
			$flag["success"] = false;
			$flag["data"]["errors"] = $translate->translate("Error");
		}
		return 	Zend_Json::encode($flag);
	}
	
	protected function doUpdateRolePrivileges($json){
		
		$arr_data = Zend_Json::decode($json, Zend_Json::TYPE_OBJECT);
		
		$vn_roleId= $arr_data->roleId;
		$vc_menuPrivilegeId = $arr_data->privilegeId;
		
		if($vc_menuPrivilegeId){
			$arr_menuPrivilegeId = explode(",",$vc_menuPrivilegeId);
			
			$lbl_roleMenuPrivilege = new Models_RoleMenuPrivilege();
			
			$vc_where = $lbl_roleMenuPrivilege->getAdapter()->quoteInto('role_id = ?', $vn_roleId);
			$result = $lbl_roleMenuPrivilege->delete($vc_where);	

			foreach($arr_menuPrivilegeId as $key=>$value){
				$data = array(
					 "role_id"=>$vn_roleId
					,"menu_privilege_id"=>$value					
				);
				$vc_result= $lbl_roleMenuPrivilege->insert($data);
			}
			$arr_flag["success"] = true;
		}else{
			$translate=Zend_Registry::get('translate');
			$arr_flag["success"] = false;
			$arr_flag["data"]["errors"] = $translate->translate("Error");
		}
		return 	Zend_Json::encode($arr_flag);
	}
}